package com.swak.mmap;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 简单的 MappedFile 应用
 * 
 * @author DELL
 */
public class MappedFileApp {

	public static void main(String[] args) throws IOException {
		int fileSize = 1024 * 1024 * 10;
		int maxRecordSize = 1024 * 1024 * 1;
		MappedFile file = new MappedFile("D:\\mmap\\raid\\000001", 1, fileSize, maxRecordSize);
		file.recover();

		System.out.println(file.toString());
		System.out.println("开始写入数据：");
		for (int i = 0; i < 100; i++) {
			byte[] data = new byte[1024 * 1024];
			AppendResult ressult = file.appendData(data);
			if (ressult.getStatus() != AppendStatus.PUT_OK) {
				break;
			}
			System.out.println("写入数据:" + ressult.getWroteBytes());
		}
		System.out.println("写入数据结束.");
		System.out.println(file.toString());

		System.out.println("####### 分隔  #########");
		MappedFileScan scan = file.scan(0, 10);
		System.out.println(scan.getRecords());
		final AtomicLong totalSize = new AtomicLong();
		scan.getRecords().forEach(record -> {
			totalSize.addAndGet(record.recordLength());
			System.out.println(record.recordLength() + ":" + record.isBlankRecord());
		});
		System.out.println("总大小：" + totalSize.get());
	}
}
